package repositories

import (
	"gitee.com/tq1024/wechat-sdk-go/dbconnect"
	"time"
)

type OpenPlatform struct {
}

var OpenPlatformReposition OpenPlatform

func init() {
	OpenPlatformReposition = OpenPlatform{}
}

func (OpenPlatform) GetById(id int) (map[string]interface{}, error) {

	sql := "select * from `open_platforms` where `id` = ?"
	return dbconnent.GetFirstRecord(sql, id)

}

func (OpenPlatform) GetByAppId(appId string) (map[string]interface{}, error) {

	sql := "select * from `open_platforms` where `appid` = ?"

	return dbconnent.GetFirstRecord(sql, appId)

}

func (OpenPlatform) SaveTicket(appId string, ticket string) (int64, error) {

	sql := "update `open_platforms` set `ticket` = ?, ticket_updated_at = ? where `appid` = ?"

	result, err := dbconnent.Exec(sql, ticket, time.Now(), appId)

	if nil != err {

		return 0, err

	}

	count, err := result.RowsAffected()

	if nil != err {
		return 0, err
	}

	return count, nil

}
